﻿using System.Reflection;

namespace AbpCodeGenerater.Core
{
    public class TemplateModel
    {
        private AbpDomainClass _abpDomainClass;

        public TemplateModel(AbpDomainClass abpDomainClass)
        {
            _abpDomainClass = abpDomainClass;
        }

        /// <summary>
        /// 类名
        /// </summary>
        public string? ClassName => _abpDomainClass?.Name;

        /// <summary>
        /// 类注释
        /// </summary>
        public string? ClassSummary => _abpDomainClass?.ClassSummary;

        /// <summary>
        /// 命名空间前缀
        /// </summary>
        public string? NamespacePrefix => _abpDomainClass?.DomainProject?.Name?.Replace(".Domain", "");

        /// <summary>
        /// 命名空间
        /// </summary>
        public string? Namespace => $"{NamespacePrefix}.{_abpDomainClass?.DirectoryPath?.TrimEnd('\\').Replace('\\', '.')}";

        /// <summary>
        /// 项目名简称
        /// </summary>
        public string? ProjectGroupName
        {
            get
            {
                if (string.IsNullOrWhiteSpace(NamespacePrefix)) return null;
                var lastPointIndex = NamespacePrefix.LastIndexOf('.');
                if (lastPointIndex == -1) return null;

                return NamespacePrefix.Substring(lastPointIndex + 1);
            }
        }

        /// <summary>
        /// 父级菜单
        /// </summary>
        public string? ParentMenu
        {
            get
            {
                return _abpDomainClass?.DirectoryPath?.TrimEnd('\\').Replace('\\', '.');
            }
        }

        /// <summary>
        /// 模型的数据库定义数据
        /// </summary>
        public List<ModelDefine> ModelDefines
        {
            get
            {
                var modelDefines = new List<ModelDefine>();

                var propertys = _abpDomainClass.SharedClassType?.GetProperties();
                if (propertys != null)
                {
                    foreach (var property in propertys)
                    {
                        var modelDefine = property.ToModelDefine();
                        if (modelDefine != null)
                        {
                            if (_abpDomainClass.ClassType.GetProperties().Any(x => x.Name == modelDefine.Name))
                            {
                                modelDefines.Add(modelDefine);
                            }
                        }
                    }
                }

                return modelDefines;
            }
        }

        public List<PropertyInfo> Propertys
        {
            get
            {
                var propertys = new List<PropertyInfo>();

                foreach (var property in _abpDomainClass.ClassType.GetProperties())
                {
                    propertys.Add(property);
                }

                return propertys;
            }
        }

        public List<PropertyInfo> SelfPropertys
        {
            get
            {
                var propertys = new List<PropertyInfo>();

                foreach (var property in _abpDomainClass.ClassType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance))
                {
                    propertys.Add(property);
                }

                return propertys;
            }
        }

        /// <summary>
        /// 类型的值属性集合
        /// </summary>
        public List<PropertyInfo> ValuePropertys
        {
            get
            {
                var navigationPropertys = new List<PropertyInfo>();

                var propertys = _abpDomainClass.ClassType.GetProperties();
                if (propertys != null)
                {
                    foreach (var property in propertys)
                    {
                        if (property.IsValueProperty())
                        {
                            navigationPropertys.Add(property);
                        }
                    }
                }

                return navigationPropertys;
            }
        }

        /// <summary>
        /// 类型的值属性集合
        /// </summary>
        public List<PropertyInfo> SelfValuePropertys
        {
            get
            {
                var navigationPropertys = new List<PropertyInfo>();

                var propertys = _abpDomainClass.ClassType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
                if (propertys != null)
                {
                    foreach (var property in propertys)
                    {
                        if (property.IsValueProperty())
                        {
                            navigationPropertys.Add(property);
                        }
                    }
                }

                return navigationPropertys;
            }
        }

        /// <summary>
        /// 类型的导航属性集合
        /// </summary>
        public List<PropertyInfo> NavigationPropertys
        {
            get
            {
                var navigationPropertys = new List<PropertyInfo>();

                var propertys = _abpDomainClass.ClassType.GetProperties();
                if (propertys != null)
                {
                    foreach (var property in propertys)
                    {
                        if (property.IsNavigationProperty())
                        {
                            navigationPropertys.Add(property);
                        }
                    }
                }

                return navigationPropertys;
            }
        }

        /// <summary>
        /// 类型的导航属性集合
        /// </summary>
        public List<PropertyInfo> SelfNavigationPropertys
        {
            get
            {
                var navigationPropertys = new List<PropertyInfo>();

                var propertys = _abpDomainClass.ClassType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
                if (propertys != null)
                {
                    foreach (var property in propertys)
                    {
                        if (property.IsNavigationProperty())
                        {
                            navigationPropertys.Add(property);
                        }
                    }
                }

                return navigationPropertys;
            }
        }

        /// <summary>
        /// 类型的列表属性集合
        /// </summary>
        public List<PropertyInfo> ArrayPropertys
        {
            get
            {
                var navigationPropertys = new List<PropertyInfo>();

                var propertys = _abpDomainClass.ClassType.GetProperties();
                if (propertys != null)
                {
                    foreach (var property in propertys)
                    {
                        if (property.IsArrayProperty())
                        {
                            navigationPropertys.Add(property);
                        }
                    }
                }

                return navigationPropertys;
            }
        }

        /// <summary>
        /// 类型的列表属性集合
        /// </summary>
        public List<PropertyInfo> SelfArrayPropertys
        {
            get
            {
                var navigationPropertys = new List<PropertyInfo>();

                var propertys = _abpDomainClass.ClassType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
                if (propertys != null)
                {
                    foreach (var property in propertys)
                    {
                        if (property.IsArrayProperty())
                        {
                            navigationPropertys.Add(property);
                        }
                    }
                }

                return navigationPropertys;
            }
        }

        public Dictionary<string, string> PropertySummaries
        {
            get
            {
                return _abpDomainClass.PropertySummaries;
            }
        }
    }
}
